package cn.itsub.demo.game;

import cn.itsub.sodo.Node;
import cn.itsub.sodo.Scene;
import cn.itsub.sodo.Sprite;
import cn.itsub.sodo.action.interval.MoveTo;
import cn.itsub.sodo.audio.SoundEngine;
import cn.itsub.sodo.core.Director;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


public class MyScene extends Scene {
	final Director dir = Director.getInstance();

	public MyScene() {
		init();
	}
	
	public void init(){
		
		Node a = new Node();
		a.setPosition(500, 100);
		
		Node b = new Node();
		b.setPosition(100, 100);

		Node c = new Node();
		c.setPosition(-200, 100);
		
		a.addChild(b);
		b.addChild(c);
		
		this.addChild(a);
		
		System.out.println(c.getWorldPosition());
		
		final Scene scene = this;
		
		new SoundEngine().playBackgroundMusic("music/QQ三国 - 青鸟鸣月.mp3", true);
		
		final Sprite bg2 = new Sprite("横版地图5.png");
		bg2.position.x = bg2.getSize().width/2;
		bg2.position.y = dir.view.getDesignSize().height/2;
		bg2.setPriority(-1);
		this.addChild(bg2);
		bg2.setScaleY(bg2.getTexture().getHeight()/Director.getInstance().getView().getFrame().getHeight());
		bg2.setScale(3);
		
		final Sprite bg3 = new Sprite("横版地图5_2.png");
		bg3.getPosition().x = 0;
		bg3.getPosition().y = 0;
		bg3.setScale(1.5f);
		bg3.setPriority(-2);
		this.addChild(bg3);
		
		final Hero shana = new Hero();
		shana.setPositionX(400);
		shana.setPositionY(380);
		shana.setScale(2f);
		shana.setAnchorX(0.5f);
		shana.setAnchorY(0.5f);
		shana.setPriority(1);
		shana.setRotation(0);
		//shana.setOpacity(0.5f);
		this.addChild(shana,10);
		
		shana.idle();


		final Frame frame = Director.view.getFrame();
		frame.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				switch (e.getKeyCode()) {
				case KeyEvent.VK_UP:

					break;
				case KeyEvent.VK_DOWN:

					break;
				case KeyEvent.VK_RIGHT:
					shana.dir=2;
					shana.run();
					break;
				case KeyEvent.VK_LEFT:
					shana.dir=1;
					shana.run();
					break;
				case KeyEvent.VK_S:
					//dir.pause();
					break;
				case KeyEvent.VK_R:
					//dir.resume();
					break;
				case KeyEvent.VK_X:
					shana.attack();
					System.out.println(shana.getWorldPosition());
					break;
				case KeyEvent.VK_Z:
					MoveTo mv = MoveTo.action(1f, (float)Math.random()*800, (float)Math.random()*600);
					shana.runAction(mv);
					break;
				case KeyEvent.VK_V:
					shana.speed=300;
					break;
				default:
					break;
				}
			}

			@Override
			public void keyReleased(KeyEvent e) {
				switch (e.getKeyCode()) {
					case KeyEvent.VK_RIGHT:
					case KeyEvent.VK_LEFT:
						shana.stop();
						shana.idle();
						break;
				}
			}
		});
		
		

	}}
